package com.qfedu.yanglao20app.companionapp.service.impl;

import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.common.utils.JwtUtils;
import com.qfedu.yanglao20.entity.companion.Companion;
import com.qfedu.yanglao20app.companionapp.dao.CompanionDao;
import com.qfedu.yanglao20app.companionapp.service.CompanionService;
import com.qfedu.yanglao20app.companionapp.vo.LoginCompanion;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanionServiceImpl implements CompanionService {

    @Resource
    private CompanionDao companionDao;

    /**
     * 陪护员登录功能
     *
     * @param loginCompanion 陪护员用户名密码
     * @return
     */
    @Override
    public String login(LoginCompanion loginCompanion) {
        Companion companion1 = companionDao.selectByUserName(loginCompanion.getUsername());
        if (companion1 == null) {
            throw new CommonException(CommonConstants.ERROR, "用户名错误");
        }
        if (!companion1.getPassword().equals(loginCompanion.getPassword())) {
            throw new CommonException(CommonConstants.ERROR, "密码错误");
        }
        // jwt
        Map<String, Object> map = new HashMap<>();
        map.put("username", loginCompanion.getUsername());
        String jwt = JwtUtils.createJwt(map);
        return jwt;
    }

    /**
     * 查询转单陪护员下拉列表
     *
     * @param id 当前陪护员id
     * @return 陪护员列表
     */
    @Override
    public List<Companion> selectAll(Integer id) {
        return companionDao.selectAll(id);
    }
}
